package com.zwps.biz.controller.http;


import com.zwps.biz.api.model.dto.dict.AddDictDTO;
import com.zwps.biz.api.model.dto.dict.SelectDictTreeDTO;
import com.zwps.biz.api.model.dto.dict.UpdateDictDTO;
import com.zwps.biz.api.model.vo.dict.DictTreeVO;
import com.zwps.biz.domain.service.DictService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;


@Tag(name = "字典管理")
@RestController
@RequestMapping("system/dict")
@Validated
@AllArgsConstructor
public class DictController {

    private DictService dictService;


    @Operation(summary = "新增字典")
    @PostMapping
    public void addDict(@Valid @NotNull @RequestBody AddDictDTO dto) {
        dictService.addDict(dto);
    }

    @Parameter(name = "parentId", description = "上级字典ID")
    @Parameter(name = "countUserNum", description = "是否统计用户数量（1：统计）")
    @Operation(summary = "查询字典树")
    @GetMapping("tree")
    public List<DictTreeVO> findDictTree(SelectDictTreeDTO dto) {
        return dictService.findDictTree(dto);
    }

    @Operation(summary = "删除字典")
    @DeleteMapping("{ids}")
    public void deleteDicts(@Valid @NotBlank @PathVariable String ids) {
        dictService.deleteDicts(ids);
    }

    @Parameter(name = "id", description = "字典ID", in = ParameterIn.PATH)
    @Operation(summary = "修改字典信息")
    @PutMapping("{id}")
    public void updateDict(@Valid @NotBlank @PathVariable String id, @Valid @NotNull @RequestBody UpdateDictDTO dto) {
        dictService.updateDict(id, dto);
    }

    @Parameter(name = "parentId",  description = "字典ID", in = ParameterIn.PATH)
    @Operation(summary = "根据父id查询子级（非树状）")
    @GetMapping("tree/{parentId}")
    public List<DictTreeVO> findDictByPid(@Valid @NotBlank @PathVariable String parentId) {
        return dictService.findDictByPid(parentId);
    }

}
